package farmer.game;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import core.game.StateBasedGame;
import core.state.BasicState;

public class LoadingScreenState extends BasicState{

	private Image loadingImage;
	private boolean updateState = true;
	private double totalTime = 5*1000;
	private double introTime = totalTime;
	private double animationX = 0;
	private int animationSize = 15;
	private int length = 0;
	private int width = 519;
	private int x = 153;
	private ArrayList<Double> animationXList = new ArrayList<Double>();
	
	public LoadingScreenState(StateBasedGame game, int stateID) {
		super(game, stateID);
	}

	@Override
	public void enter() {
		
	}

	@Override
	public void init() {
		try {
			loadingImage = ImageIO.read(new File("loadingScreen.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		for(int i=0; i<width/animationSize; i++){
			animationXList.add((double) (x+i*animationSize*2));
		}
	}

	@Override
	public void render(Graphics2D g) {
		if(loadingImage != null){
			g.drawImage(loadingImage, 0, 0, null);
		}else{
			g.setColor(Color.darkGray);
			g.fillRect(0, 0, 800, 600);
		}
		//Draw loading bar
		int y = 461;
		int height = 25;
		Color greenColor = new Color(6, 172, 15);
		g.setColor(new Color(1, 1, 1, 1.0f));
		g.fillRect(x, y, width, height);
		g.setColor(greenColor);
		g.fillRect(x, y, length, height);
		//Draw loading animation
		g.setClip(x, y, Math.min((int)animationX, length), height);
		GradientPaint gradientColor = new GradientPaint((float) (x-(300*length/width)+animationX), y, greenColor, (float) (x+animationX), y, new Color(1, 1, 1, 0.6f));
		g.setPaint(gradientColor);
		//g.fillRect(x, y, width, height);
		
		//Draw loading animation 2
		g.setClip(null);
		int min = Math.min(length, width-1);
		g.setClip(x, y, min, height);
		g.setColor(new Color(3, 80, 7, 50));
		for(int i=0; i<animationXList.size(); i++){
			double d = animationXList.get(i);
			g.fillRect((int) d, y, 15, height);
		}
	}

	@Override
	public void update(int deltaTime) {
		if(updateState){
			game.updateStates();
			updateState = false;
		}
		if(introTime > 0){
			introTime -= deltaTime;
			if(introTime <= 0){
				//game.enterState(FarmerBoyGame.MainMenu);
			}
		}else{
			game.enterState(FarmerBoyGame.MainMenu);
		}
		//Update animation
		for(int i=0; i<animationXList.size(); i++){
			double d = animationXList.get(i);
			d += deltaTime*0.085;
			if(d > width+x){
				animationXList.remove(i);
			}else{
				animationXList.set(i, d);
			}
		}
		double d = animationXList.get(0);
		if(d > x+15){
			//animationXList.add(0, (double) x-15);
			animationXList.add(0, (double) d-30);
		}
		//Essai
		length = (int) (width*((totalTime - introTime)/totalTime));
		animationX += 1.0*deltaTime*(length+200)*0.0005;
		if(animationX > length+(300*length/width)){
			animationX = 0;
		}
	}

}
